﻿using System;
using System.Xml;

namespace Builderator.Core
{
    public class Variable
    {
        public string Name
        {
            get;
            set;
        }

        public string Value
        {
            get;
            set;
        }

        public bool CanDelete
        {
            get;
            set;
        }

        public Variable()
        {
            this.CanDelete = true;
        }

        public Variable(string settings)
            : this()
        {
            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.LoadXml(settings);

            XmlElement rootElement = xmlDoc.DocumentElement;

            this.Name = rootElement.GetAttribute("Name");

            this.Value = rootElement.GetAttribute("Value");

            this.CanDelete = Boolean.Parse(rootElement.GetAttribute("CanDelete"));
        }

        public Variable(string name, string value, bool delete)
            : this()
        {
            this.Name = name;

            this.Value = value;

            this.CanDelete = delete;
        }

        public override string ToString()
        {
            XmlDocument xmlDoc = new XmlDocument();

            XmlElement rootElement = xmlDoc.CreateElement("Variable");

            rootElement.SetAttribute("Name", this.Name);

            rootElement.SetAttribute("Value", this.Value);

            rootElement.SetAttribute("CanDelete", this.CanDelete.ToString());

            xmlDoc.AppendChild(rootElement);

            return xmlDoc.InnerXml;
        }
    }
}